﻿using Aspose.Cells;
using Meiam.System.Common;
using Meiam.System.Interfaces.IService;
using Meiam.System.Interfaces.IService.bussiness;
using Meiam.System.Model.Entity;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;



namespace Meiam.System.Interfaces
{
    public class SendMailService : ISendMailService
    {
        public readonly IYieldSummaryService _YieldSummaryService;
        public readonly IEquipmentDropBoxService _EquipmentDropBoxService;
        public readonly IProportionSummaryService _ProportionSummaryService;
        public readonly IDropboxIndexWorkShopService _DropboxIndexWorkShopService;
        public readonly ICutPieceTotalNumService _CutPieceTotalNumService;
        public readonly IMachineConfirmationService _MachineConfirmationService;
        public readonly ICutPieceEfficiencySummaryService _CutPieceEfficiencySummaryService;

        public SendMailService(ICutPieceTotalNumService cutPieceTotalNumService,IEquipmentDropBoxService equipmentDropBoxService, IYieldSummaryService yieldSummaryService, IProportionSummaryService proportionSummaryService,IDropboxIndexWorkShopService dropboxIndexWorkShopService, ICutPieceEfficiencySummaryService cutPieceEfficiencySummaryService, IMachineConfirmationService machineConfirmationService)
        {
            _YieldSummaryService = yieldSummaryService;
            _CutPieceTotalNumService = cutPieceTotalNumService;
            _EquipmentDropBoxService = equipmentDropBoxService;
            _ProportionSummaryService = proportionSummaryService;
            _MachineConfirmationService =  machineConfirmationService;
            _DropboxIndexWorkShopService = dropboxIndexWorkShopService;
            _CutPieceEfficiencySummaryService = cutPieceEfficiencySummaryService;
        }

        public async Task<bool> Run(string param)
        {
            var paramlist = param.Split(',');
            var command = Convert.ToInt32(paramlist[0]);

            string currentDirectory = AppContext.BaseDirectory;
            string dirPredix = "resources";
            string templatePredix = "Template";

            DateTime dtNow = DateTime.Now;
            DateTime dtNowPrev = dtNow.AddHours(-2);
            string startTime = dtNowPrev.ToShortDateString() + " " + (dtNowPrev.Hour > 9 ? dtNowPrev.Hour.ToString() : (string.Format("{0}{1}", "0", dtNowPrev.Hour))) + ":00:00";
            string endTime = dtNow.ToShortDateString() + " " + (dtNow.Hour > 9 ? dtNow.Hour.ToString() : (string.Format("{0}{1}", "0", dtNow.Hour))) + ":00:00";

            bool excelResult = false;
            if (command != 7) return false;

            await Task.Run(() =>
            {
                if (command == 0)
                {
                    string scadaFileName = "落盒关键指标(一车间)2小时" + ".xlsx";
                    string scadaFilePath = currentDirectory  + dirPredix+"\\"+ scadaFileName;

                    var list= _DropboxIndexWorkShopService.GetWorkshopOneData2H();
                    excelResult = ExcelHelper<DropboxIndexWorkShop>.SaveExcel(list, scadaFilePath, ExcelVersion.xlsx);

                    if (excelResult)
                    {
                        SendMail(new SendMailModel()
                        {
                            SendType=1,
                            PushCode= "567",
                            Type = "落盒关键指标(一车间)2小时",
                            Title = $"自动报表推送.落盒关键指标(一车间)2小时",
                            Content = string.Format("{0}:{1}-{2}", "时间范围",startTime,endTime),
                            Attachment = scadaFilePath
                        });
                    }
                }
                else if (command == 1)
                {
                    string scadaFileName = "落盒关键指标(二车间)2小时" + ".xlsx";
                    string scadaFilePath = currentDirectory + dirPredix + "\\" + scadaFileName;

                    var list = _DropboxIndexWorkShopService.GetWorkshopTwoData2H();
                    excelResult = ExcelHelper<DropboxIndexWorkShop>.SaveExcel(list, scadaFilePath, ExcelVersion.xlsx);

                    if (excelResult)
                    {
                        SendMail(new SendMailModel()
                        {
                            SendType = 1,
                            PushCode = "567",
                            Type = "落盒关键指标(二车间)2小时",
                            Title = $"自动报表推送.落盒关键指标(二车间)2小时",
                            Content = string.Format("{0}:{1}-{2}", "时间范围", startTime, endTime),
                            Attachment = scadaFilePath
                        });
                    }
                }
                else if (command == 2)
                {
                    string scadaFileName = "落盒关键指标(三车间)2小时" + ".xlsx";
                    string scadaFilePath = currentDirectory + dirPredix + "\\" + scadaFileName;

                    var list = _DropboxIndexWorkShopService.GetWorkshopThreeData2H();
                    excelResult = ExcelHelper<DropboxIndexWorkShop>.SaveExcel(list, scadaFilePath, ExcelVersion.xlsx);

                    if (excelResult)
                    {
                        SendMail(new SendMailModel()
                        {
                            SendType = 1,
                            PushCode = "567",
                            Type = "落盒关键指标(三车间)2小时",
                            Title = $"自动报表推送.落盒关键指标(三车间)2小时",
                            Content = string.Format("{0}:{1}-{2}", "时间范围", startTime, endTime),
                            Attachment = scadaFilePath
                        });
                    }
                }
                else if (command == 3)
                {
                    DateTime dtNow = DateTime.Now;
                    DateTime dtNowPrev = dtNow.AddHours(-4);
                    string startTime = dtNowPrev.ToShortDateString() + " " + (dtNowPrev.Hour > 9 ? dtNowPrev.Hour.ToString() : (string.Format("{0}{1}", "0", dtNowPrev.Hour))) + ":00:00";
                    string endTime = dtNow.ToShortDateString() + " " + (dtNow.Hour > 9 ? dtNow.Hour.ToString() : (string.Format("{0}{1}", "0", dtNow.Hour))) + ":00:00";

                    string scadaFileName = "切片效率汇总报表-4H" + ".xlsx";
                    string scadaFilePath = currentDirectory + dirPredix + "\\" + scadaFileName;

                    var list = _CutPieceEfficiencySummaryService.GetCutPieceEfficiencySummaryData4H();
                    excelResult = ExcelHelper<CutPieceEfficiencySummary>.SaveExcel(list, scadaFilePath, ExcelVersion.xlsx);

                    if (excelResult)
                    {
                        SendMail(new SendMailModel()
                        {
                            SendType = 1,
                            PushCode = "525",
                            Type = "切片效率汇总报表-4H",
                            Title = $"自动报表推送.切片效率汇总报表-4H",
                            Content = string.Format("{0}:{1}-{2}", "时间范围", startTime, endTime),
                            Attachment = scadaFilePath
                        });
                    }
                }
                else if(command == 4)
                {
                    DateTime dtNow = DateTime.Now;
                    DateTime dtNowPrev = dtNow.AddHours(-8);
                    string startTime = dtNowPrev.ToShortDateString() + " " + (dtNowPrev.Hour > 9 ? dtNowPrev.Hour.ToString() : (string.Format("{0}{1}", "0", dtNowPrev.Hour))) + ":00:00";
                    string endTime = dtNow.ToShortDateString() + " " + (dtNow.Hour > 9 ? dtNow.Hour.ToString() : (string.Format("{0}{1}", "0", dtNow.Hour))) + ":00:00";

                    string scadaFileName = "切片效率汇总报表-8H" + ".xlsx";
                    string scadaFilePath = currentDirectory + dirPredix + "\\" + scadaFileName;

                    var list = _CutPieceEfficiencySummaryService.GetCutPieceEfficiencySummaryData8H();
                    excelResult = ExcelHelper<CutPieceEfficiencySummary>.SaveExcel(list, scadaFilePath, ExcelVersion.xlsx);

                    if (excelResult)
                    {
                        SendMail(new SendMailModel()
                        {
                            SendType = 1,
                            PushCode = "525",
                            Type = "切片效率汇总报表-8H",
                            Title = $"自动报表推送.切片效率汇总报表-8H",
                            Content = string.Format("{0}:{1}-{2}", "时间范围", startTime, endTime),
                            Attachment = scadaFilePath
                        });
                    }
                }
                else if (command == 5)
                {
                    DateTime dtNow = DateTime.Now;
                    DateTime dtNowPrev = dtNow.AddHours(-12);
                    string startTime = dtNowPrev.ToShortDateString() + " " + (dtNowPrev.Hour > 9 ? dtNowPrev.Hour.ToString() : (string.Format("{0}{1}", "0", dtNowPrev.Hour))) + ":00:00";
                    string endTime = dtNow.ToShortDateString() + " " + (dtNow.Hour > 9 ? dtNow.Hour.ToString() : (string.Format("{0}{1}", "0", dtNow.Hour))) + ":00:00";

                    string scadaFileName = "切片效率汇总报表-12H" + ".xlsx";
                    string scadaFilePath = currentDirectory + dirPredix + "\\" + scadaFileName;

                    var list = _CutPieceEfficiencySummaryService.GetCutPieceEfficiencySummaryData12H();
                    excelResult = ExcelHelper<CutPieceEfficiencySummary>.SaveExcel(list, scadaFilePath, ExcelVersion.xlsx);

                    if (excelResult)
                    {
                        SendMail(new SendMailModel()
                        {
                            SendType = 1,
                            PushCode = "525",
                            Type = "切片效率汇总报表-12H",
                            Title = $"自动报表推送.切片效率汇总报表-12H",
                            Content = string.Format("{0}:{1}-{2}", "时间范围", startTime, endTime),
                            Attachment = scadaFilePath
                        });
                    }
                }
                else if (command == 6)
                {
                    DateTime dtNow = DateTime.Now;
                    DateTime dtNowPrev = dtNow.AddHours(-24);
                    string startTime = dtNowPrev.ToShortDateString() + " " + (dtNowPrev.Hour > 9 ? dtNowPrev.Hour.ToString() : (string.Format("{0}{1}", "0", dtNowPrev.Hour))) + ":00:00";
                    string endTime = dtNow.ToShortDateString() + " " + (dtNow.Hour > 9 ? dtNow.Hour.ToString() : (string.Format("{0}{1}", "0", dtNow.Hour))) + ":00:00";

                    string scadaFileName = "切片效率汇总报表-24H" + ".xlsx";
                    string scadaFilePath = currentDirectory + dirPredix + "\\" + scadaFileName;

                    var list = _CutPieceEfficiencySummaryService.GetCutPieceEfficiencySummaryData24H();
                    excelResult = ExcelHelper<CutPieceEfficiencySummary>.SaveExcel(list, scadaFilePath, ExcelVersion.xlsx);

                    if (excelResult)
                    {
                        SendMail(new SendMailModel()
                        {
                            SendType = 1,
                            PushCode = "525",
                            Type = "切片效率汇总报表-24H",
                            Title = $"自动报表推送.切片效率汇总报表-24H",
                            Content = string.Format("{0}:{1}-{2}", "时间范围", startTime, endTime),
                            Attachment = scadaFilePath
                        });
                    }
                }
                else if (command == 7)
                {
                    DateTime dtNow = DateTime.Now;
                    DateTime dtNowPrev = dtNow.AddHours(-12);
                    string startTime = dtNowPrev.ToShortDateString() + " " + (dtNowPrev.Hour > 9 ? dtNowPrev.Hour.ToString() : (string.Format("{0}{1}", "0", dtNowPrev.Hour))) + ":00:00";
                    string endTime = dtNow.ToShortDateString() + " " + (dtNow.Hour > 9 ? dtNow.Hour.ToString() : (string.Format("{0}{1}", "0", dtNow.Hour))) + ":00:00";

                    string scadaFileName = "落盒关键指标(一车间)12小时" + ".xlsx";
                    string scadaFilePath = currentDirectory + dirPredix + "\\" + scadaFileName;

                    //var list = _DropboxIndexWorkShopService.GetWorkshopOneData12H();

                    //excelResult = ExcelHelper<DropboxIndexWorkShop>.SaveExcel(list, scadaFilePath, ExcelVersion.xlsx);

                    string timestamp = DateTime.Now.ToString("yyyyMMddHHmmss");
                    string pngName = Path.GetFileNameWithoutExtension(scadaFilePath) + "_" + timestamp + ".png";
                    string pngPath = Path.GetDirectoryName(scadaFilePath) + "\\" + pngName;

                    //Thread.Sleep(1500);
                    MailHelper.ExportPng(pngPath, scadaFilePath);
                    if (excelResult)
                    {
                        //Thread.Sleep(1500);
                        //MailHelper.ExportPng(pngPath, scadaFilePath);
                        //SendMail(new SendMailModel()
                        //{
                        //    SendType = 1,
                        //    PushCode = "567",
                        //    Type = "落盒关键指标(一车间)12小时",
                        //    Title = $"自动报表推送.落盒关键指标(一车间)12小时",
                        //    Content = string.Format("{0}:{1}-{2}", "时间范围", startTime, endTime),
                        //    Attachment = scadaFilePath
                        //});
                    }
                }
                else if (command == 8)
                {
                    DateTime dtNow = DateTime.Now;
                    DateTime dtNowPrev = dtNow.AddHours(-12);
                    string startTime = dtNowPrev.ToShortDateString() + " " + (dtNowPrev.Hour > 9 ? dtNowPrev.Hour.ToString() : (string.Format("{0}{1}", "0", dtNowPrev.Hour))) + ":00:00";
                    string endTime = dtNow.ToShortDateString() + " " + (dtNow.Hour > 9 ? dtNow.Hour.ToString() : (string.Format("{0}{1}", "0", dtNow.Hour))) + ":00:00";

                    string scadaFileName = "落盒关键指标(二车间)12小时" + ".xlsx";
                    string scadaFilePath = currentDirectory + dirPredix + "\\" + scadaFileName;

                    var list = _DropboxIndexWorkShopService.GetWorkshopTwoData12H();
                    excelResult = ExcelHelper<DropboxIndexWorkShop>.SaveExcel(list, scadaFilePath, ExcelVersion.xlsx);

                    if (excelResult)
                    {
                        SendMail(new SendMailModel()
                        {
                            SendType = 1,
                            PushCode = "567",
                            Type = "落盒关键指标(二车间)12小时",
                            Title = $"自动报表推送.落盒关键指标(二车间)12小时",
                            Content = string.Format("{0}:{1}-{2}", "时间范围", startTime, endTime),
                            Attachment = scadaFilePath
                        });
                    }
                }
                else if (command == 9)
                {
                    DateTime dtNow = DateTime.Now;
                    DateTime dtNowPrev = dtNow.AddHours(-12);
                    string startTime = dtNowPrev.ToShortDateString() + " " + (dtNowPrev.Hour > 9 ? dtNowPrev.Hour.ToString() : (string.Format("{0}{1}", "0", dtNowPrev.Hour))) + ":00:00";
                    string endTime = dtNow.ToShortDateString() + " " + (dtNow.Hour > 9 ? dtNow.Hour.ToString() : (string.Format("{0}{1}", "0", dtNow.Hour))) + ":00:00";

                    string scadaFileName = "落盒关键指标(三车间)12小时" + ".xlsx";
                    string scadaFilePath = currentDirectory + dirPredix + "\\" + scadaFileName;

                    var list = _DropboxIndexWorkShopService.GetWorkshopThreeData12H();
                    excelResult = ExcelHelper<DropboxIndexWorkShop>.SaveExcel(list, scadaFilePath, ExcelVersion.xlsx);

                    if (excelResult)
                    {
                        SendMail(new SendMailModel()
                        {
                            SendType = 1,
                            PushCode = "567",
                            Type = "落盒关键指标(三车间)12小时",
                            Title = $"自动报表推送.落盒关键指标(三车间)12小时",
                            Content = string.Format("{0}:{1}-{2}", "时间范围", startTime, endTime),
                            Attachment = scadaFilePath
                        });
                    }
                }
                else if (command == 10)
                {
                    DateTime dtNow = DateTime.Now;
                    DateTime dtNowPrev = dtNow.AddHours(-12);
                    string startTime = dtNowPrev.ToShortDateString() + " " + (dtNowPrev.Hour > 9 ? dtNowPrev.Hour.ToString() : (string.Format("{0}{1}", "0", dtNowPrev.Hour))) + ":00:00";
                    string endTime = dtNow.ToShortDateString() + " " + (dtNow.Hour > 9 ? dtNow.Hour.ToString() : (string.Format("{0}{1}", "0", dtNow.Hour))) + ":00:00";

                    string scadaFileName = "切片刀数断线汇总12小时" + ".xlsx";
                    string scadaFilePath = currentDirectory + dirPredix + "\\" + scadaFileName;

                    var list = _CutPieceTotalNumService.GetSummaryData12H();
                    excelResult = ExcelHelper<CutPieceTotalNum>.SaveExcel(list, scadaFilePath, ExcelVersion.xlsx);

                    if (excelResult)
                    {
                        SendMail(new SendMailModel()
                        {
                            SendType = 1,
                            PushCode = "525",
                            Type = "切片刀数断线汇总12小时",
                            Title = $"自动报表推送.切片刀数断线汇总12小时",
                            Content = string.Format("{0}:{1}-{2}", "时间范围", startTime, endTime),
                            Attachment = scadaFilePath
                        });
                    }
                }
                else if(command ==11)
                {
                    DateTime dtNow = DateTime.Now;
                    DateTime dtNowPrev = dtNow.AddHours(-4);
                    string startTime = dtNowPrev.ToShortDateString() + " " + (dtNowPrev.Hour > 9 ? dtNowPrev.Hour.ToString() : (string.Format("{0}{1}", "0", dtNowPrev.Hour))) + ":00:00";
                    string endTime = dtNow.ToShortDateString() + " " + (dtNow.Hour > 9 ? dtNow.Hour.ToString() : (string.Format("{0}{1}", "0", dtNow.Hour))) + ":00:00";
                    string scadaFileName = "切片关键指标(一车间A区)4小时" + ".xlsx";
                    string scadaFilePath = currentDirectory + dirPredix + "\\" + scadaFileName;

                    var list = _MachineConfirmationService.GetMachineConfirmationWorkshopOneAreaA4H();
                    excelResult = ExcelHelper<MachineConfirmation>.SaveExcel(list, scadaFilePath, ExcelVersion.xlsx);

                    if (excelResult)
                    {
                        SendMail(new SendMailModel()
                        {
                            SendType = 1,
                            PushCode = "525",
                            Type = "切片关键指标(一车间A区)4小时",
                            Title = $"自动报表推送.切片关键指标(一车间A区)4小时",
                            Content = string.Format("{0}:{1}-{2}", "时间范围", startTime, endTime),
                            Attachment = scadaFilePath
                        });
                    }
                }
                else if (command == 12)
                {
                    DateTime dtNow = DateTime.Now;
                    DateTime dtNowPrev = dtNow.AddHours(-4);
                    string startTime = dtNowPrev.ToShortDateString() + " " + (dtNowPrev.Hour > 9 ? dtNowPrev.Hour.ToString() : (string.Format("{0}{1}", "0", dtNowPrev.Hour))) + ":00:00";
                    string endTime = dtNow.ToShortDateString() + " " + (dtNow.Hour > 9 ? dtNow.Hour.ToString() : (string.Format("{0}{1}", "0", dtNow.Hour))) + ":00:00";
                    string scadaFileName = "切片关键指标(一车间B区)4小时" + ".xlsx";
                    string scadaFilePath = currentDirectory + dirPredix + "\\" + scadaFileName;

                    var list = _MachineConfirmationService.GetMachineConfirmationWorkshopOneAreaB4H();
                    excelResult = ExcelHelper<MachineConfirmation>.SaveExcel(list, scadaFilePath, ExcelVersion.xlsx);

                    if (excelResult)
                    {
                        SendMail(new SendMailModel()
                        {
                            SendType = 1,
                            PushCode = "525",
                            Type = "切片关键指标(一车间B区)4小时",
                            Title = $"自动报表推送.切片关键指标(一车间B区)4小时",
                            Content = string.Format("{0}:{1}-{2}", "时间范围", startTime, endTime),
                            Attachment = scadaFilePath
                        });
                    }
                }
                else if (command == 13)
                {
                    DateTime dtNow = DateTime.Now;
                    DateTime dtNowPrev = dtNow.AddHours(-4);
                    string startTime = dtNowPrev.ToShortDateString() + " " + (dtNowPrev.Hour > 9 ? dtNowPrev.Hour.ToString() : (string.Format("{0}{1}", "0", dtNowPrev.Hour))) + ":00:00";
                    string endTime = dtNow.ToShortDateString() + " " + (dtNow.Hour > 9 ? dtNow.Hour.ToString() : (string.Format("{0}{1}", "0", dtNow.Hour))) + ":00:00";
                    string scadaFileName = "切片关键指标(二车间A区)4小时" + ".xlsx";
                    string scadaFilePath = currentDirectory + dirPredix + "\\" + scadaFileName;

                    var list = _MachineConfirmationService.GetMachineConfirmationWorkshopTwoAreaA4H();
                    excelResult = ExcelHelper<MachineConfirmation>.SaveExcel(list, scadaFilePath, ExcelVersion.xlsx);

                    if (excelResult)
                    {
                        SendMail(new SendMailModel()
                        {
                            SendType = 1,
                            PushCode = "525",
                            Type = "切片关键指标(二车间A区)4小时",
                            Title = $"自动报表推送.切片关键指标(二车间A区)4小时",
                            Content = string.Format("{0}:{1}-{2}", "时间范围", startTime, endTime),
                            Attachment = scadaFilePath
                        });
                    }
                }
                else if (command == 14) 
                {
                    DateTime dtNow = DateTime.Now;
                    DateTime dtNowPrev = dtNow.AddHours(-4);
                    string startTime = dtNowPrev.ToShortDateString() + " " + (dtNowPrev.Hour > 9 ? dtNowPrev.Hour.ToString() : (string.Format("{0}{1}", "0", dtNowPrev.Hour))) + ":00:00";
                    string endTime = dtNow.ToShortDateString() + " " + (dtNow.Hour > 9 ? dtNow.Hour.ToString() : (string.Format("{0}{1}", "0", dtNow.Hour))) + ":00:00";
                    string scadaFileName = "切片关键指标(二车间B区)4小时" + ".xlsx";
                    string scadaFilePath = currentDirectory + dirPredix + "\\" + scadaFileName;

                    var list = _MachineConfirmationService.GetMachineConfirmationWorkshopTwoAreaB4H();
                    excelResult = ExcelHelper<MachineConfirmation>.SaveExcel(list, scadaFilePath, ExcelVersion.xlsx);

                    if (excelResult)
                    {
                        SendMail(new SendMailModel()
                        {
                            SendType = 1,
                            PushCode = "525",
                            Type = "切片关键指标(二车间B区)4小时",
                            Title = $"自动报表推送.切片关键指标(二车间B区)4小时",
                            Content = string.Format("{0}:{1}-{2}", "时间范围", startTime, endTime),
                            Attachment = scadaFilePath
                        });
                    }
                }
                else if (command == 15)
                {
                    DateTime dtNow = DateTime.Now;
                    DateTime dtNowPrev = dtNow.AddHours(-4);
                    string startTime = dtNowPrev.ToShortDateString() + " " + (dtNowPrev.Hour > 9 ? dtNowPrev.Hour.ToString() : (string.Format("{0}{1}", "0", dtNowPrev.Hour))) + ":00:00";
                    string endTime = dtNow.ToShortDateString() + " " + (dtNow.Hour > 9 ? dtNow.Hour.ToString() : (string.Format("{0}{1}", "0", dtNow.Hour))) + ":00:00";
                    string scadaFileName = "切片关键指标(三车间A区)4小时" + ".xlsx";
                    string scadaFilePath = currentDirectory + dirPredix + "\\" + scadaFileName;

                    var list = _MachineConfirmationService.GetMachineConfirmationWorkshopThreeAreaA4H();
                    excelResult = ExcelHelper<MachineConfirmation>.SaveExcel(list, scadaFilePath, ExcelVersion.xlsx);

                    if (excelResult)
                    {
                        SendMail(new SendMailModel()
                        {
                            SendType = 1,
                            PushCode = "525",
                            Type = "切片关键指标(三车间A区)4小时",
                            Title = $"自动报表推送.切片关键指标(三车间A区)4小时",
                            Content = string.Format("{0}:{1}-{2}", "时间范围", startTime, endTime),
                            Attachment = scadaFilePath
                        });
                    }
                }
                else if (command == 16)
                {
                    DateTime dtNow = DateTime.Now;
                    DateTime dtNowPrev = dtNow.AddHours(-4);
                    string startTime = dtNowPrev.ToShortDateString() + " " + (dtNowPrev.Hour > 9 ? dtNowPrev.Hour.ToString() : (string.Format("{0}{1}", "0", dtNowPrev.Hour))) + ":00:00";
                    string endTime = dtNow.ToShortDateString() + " " + (dtNow.Hour > 9 ? dtNow.Hour.ToString() : (string.Format("{0}{1}", "0", dtNow.Hour))) + ":00:00";
                    string scadaFileName = "切片关键指标(三车间B区)4小时" + ".xlsx";
                    string scadaFilePath = currentDirectory + dirPredix + "\\" + scadaFileName;

                    var list = _MachineConfirmationService.GetMachineConfirmationWorkshopThreeAreaB4H();
                    excelResult = ExcelHelper<MachineConfirmation>.SaveExcel(list, scadaFilePath, ExcelVersion.xlsx);

                    if (excelResult)
                    {
                        SendMail(new SendMailModel()
                        {
                            SendType = 1,
                            PushCode = "525",
                            Type = "切片关键指标(三车间B区)4小时",
                            Title = $"自动报表推送.切片关键指标(三车间B区)4小时",
                            Content = string.Format("{0}:{1}-{2}", "时间范围", startTime, endTime),
                            Attachment = scadaFilePath
                        });
                    }
                }
                else if (command == 17)
                {
                    DateTime dtNow = DateTime.Now;
                    DateTime dtNowPrev = dtNow.AddHours(-4);
                    string startTime = dtNowPrev.ToShortDateString() + " " + (dtNowPrev.Hour > 9 ? dtNowPrev.Hour.ToString() : (string.Format("{0}{1}", "0", dtNowPrev.Hour))) + ":00:00";
                    string endTime = dtNow.ToShortDateString() + " " + (dtNow.Hour > 9 ? dtNow.Hour.ToString() : (string.Format("{0}{1}", "0", dtNow.Hour))) + ":00:00";
                    string scadaFileName = "切片关键指标(四车间A区)4小时" + ".xlsx";
                    string scadaFilePath = currentDirectory + dirPredix + "\\" + scadaFileName;

                    var list = _MachineConfirmationService.GetMachineConfirmationWorkshopFourAreaA4H();
                    excelResult = ExcelHelper<MachineConfirmation>.SaveExcel(list, scadaFilePath, ExcelVersion.xlsx);

                    if (excelResult)
                    {
                        SendMail(new SendMailModel()
                        {
                            SendType = 1,
                            PushCode = "525",
                            Type = "切片关键指标(四车间A区)4小时",
                            Title = $"自动报表推送.切片关键指标(四车间A区)4小时",
                            Content = string.Format("{0}:{1}-{2}", "时间范围", startTime, endTime),
                            Attachment = scadaFilePath
                        });
                    }
                }
                else if (command == 18)
                {
                    DateTime dtNow = DateTime.Now;
                    DateTime dtNowPrev = dtNow.AddHours(-4);
                    string startTime = dtNowPrev.ToShortDateString() + " " + (dtNowPrev.Hour > 9 ? dtNowPrev.Hour.ToString() : (string.Format("{0}{1}", "0", dtNowPrev.Hour))) + ":00:00";
                    string endTime = dtNow.ToShortDateString() + " " + (dtNow.Hour > 9 ? dtNow.Hour.ToString() : (string.Format("{0}{1}", "0", dtNow.Hour))) + ":00:00";
                    string scadaFileName = "切片关键指标(四车间B区)4小时" + ".xlsx";
                    string scadaFilePath = currentDirectory + dirPredix + "\\" + scadaFileName;

                    var list = _MachineConfirmationService.GetMachineConfirmationWorkshopFourAreaB4H();
                    excelResult = ExcelHelper<MachineConfirmation>.SaveExcel(list, scadaFilePath, ExcelVersion.xlsx);

                    if (excelResult)
                    {
                        SendMail(new SendMailModel()
                        {
                            SendType = 1,
                            PushCode = "525",
                            Type = "切片关键指标(四车间B区)4小时",
                            Title = $"自动报表推送.切片关键指标(四车间B区)4小时",
                            Content = string.Format("{0}:{1}-{2}", "时间范围", startTime, endTime),
                            Attachment = scadaFilePath
                        });
                    }
                }
                else
                {
                    string scadaFileName = "良率汇总" + ".xlsx";
                    string scadaFilePath = currentDirectory + dirPredix + "\\" + scadaFileName;

                    var list = _YieldSummaryService.GetYieldSummaryData(paramlist[1], paramlist[2], paramlist[3]);
                    excelResult = ExcelHelper<YieldSummary>.SaveExcel(list, scadaFilePath, ExcelVersion.xlsx);

                    if (excelResult)
                    {
                        SendMail(new SendMailModel()
                        {
                            SendType = 0,
                            PushCode="",
                            Type = "良率汇总",
                            Title = $"自动报表推送.良率汇总",
                            Content = string.Format("{0},班次:{1},时间:{2}-{3}", "报表为自动推送", paramlist[1], paramlist[2], paramlist[3]),
                            Attachment = scadaFilePath
                        });
                    }

                }

            });

            return true;
        }

        public bool SendMail(SendMailModel model)
        {
            return MailHelper.SendMail(model.SendType,model.PushCode,model.Type, model.Title, model.Content, model.Attachment, model.MailInfo,model.OAInfo);
        }

    

    }
}
